package com.woniuxy.carlife.carlifeuserweb.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.carlife.carlifecommonsweb.exception.CarlifeGlobalException;
import com.woniuxy.carlife.carlifecommonsweb.util.CopyUtil;
import com.woniuxy.carlife.carlifeusermodel.dto.UserCommentDto;
import com.woniuxy.carlife.carlifeusermodel.dto.UserInfoDto;
import com.woniuxy.carlife.carlifeusermodel.param.UserInfoModifiedParam;
import com.woniuxy.carlife.carlifeusermodel.param.UserInfoParam;
import com.woniuxy.carlife.carlifeuserweb.entity.User;
import com.woniuxy.carlife.carlifeuserweb.entity.UserInfo;
import com.woniuxy.carlife.carlifeuserweb.entity.Usercar;
import com.woniuxy.carlife.carlifeuserweb.mapper.UserInfoMapper;
import com.woniuxy.carlife.carlifeuserweb.mapper.UserMapper;
import com.woniuxy.carlife.carlifeuserweb.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author zjq
 * @ClassName:UserInfoServiceImpl
 * @Description:用户详情
 * @date 2021/4/6 20:02
 **/

@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Resource
    UserInfoMapper userInfoMapper;

    @Resource
    UserService userService;

    //    购物车
    @Resource
    ShoppingcartService shoppingcartService;

    //    商品
    @Resource
    ProdectfavoritesService prodectfavoritesService;

    //    门店
    @Resource
    StorefavoritesService storefavoritesService;

    //    门店服务
    @Resource
    ServiceInfofavoritesService serviceInfofavoritesService;

    //    充电站
    @Resource
    ChargingStationfavoritesService chargingStationfavoritesService;

    //    汽车
    @Resource
    UsercarService usercarService;




    /*
     * 功能描述〈查询用户详情〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/9 22:28
     * @throws
     */

    @Override
    public UserInfo findUserInfo(Integer userId) {
        if (ObjectUtil.isNull(userId) && userId == 0) {
            return null;
        }
        // 查询条件
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("user_id", userId);
//         开始查询
        UserInfo userInfo = userInfoMapper.selectOne(userInfoQueryWrapper);

        if (ObjectUtil.isNotNull(userInfo)) {
            return userInfo;
        }

        return null;
    }

    /*
     * 功能描述〈修改用户详情信息〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/9 23:42
     * @throws
     */

    @SneakyThrows

    @Override
    public Integer modifyUserInfo(UserInfo userInfo) {

        if (ObjectUtil.isNull(userInfo)) {
            return null;
        }

        UpdateWrapper<UserInfo> wrapper = new UpdateWrapper<>();

        wrapper.eq("user_id", userInfo.getUserId());


        int update = userInfoMapper.update(userInfo, wrapper);

//        同时修改用户表中的电话
        if (ObjectUtil.isNotNull(userInfo.getUserPhone())) {

            User user = new User();
            user.setUserId(userInfo.getUserId());
            user.setUserPhone(userInfo.getUserPhone());

            userService.modifyUserById(user);

        }


        if (update > 0) {
            return 1;
        }
        return 0;
    }

    /*
     * 功能描述〈用户注册时新增用户详情信息〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/10 9:39
     * @throws
     */

    @Override
    public boolean addUserInfo(UserInfo userInfo) {

        int insert = userInfoMapper.insert(userInfo);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    /*
     * 功能描述〈查询余额根据用户id〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/15 16:57
     * @throws
     */
    @Override
    public BigDecimal findOverageByUserId(Integer userId) {
        QueryWrapper<UserInfo> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_id", userId);
        UserInfo userInfo = userInfoMapper.selectOne(userQueryWrapper);
        BigDecimal userInfoOverage = userInfo.getUserInfoOverage();
        return userInfoOverage;
    }


    /*
     * 功能描述〈用户详情页整合、收藏数、购物车数、余额、卡劵〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/15 15:11
     * @throws
     */
    @Override
    public UserInfoDto totalDetail(Integer userId) {
        UserInfoDto userInfoDto = new UserInfoDto();
//        收藏数量相加
        Integer integer1 = prodectfavoritesService.prodectfavoritesTotalByUserId(userId);
        Integer integer2 = storefavoritesService.storefavoritesTotalByUserId(userId);
        Integer integer3 = serviceInfofavoritesService.serviceInfofavoritesTotalByUserId(userId);
        Integer integer4 = chargingStationfavoritesService.chargingStationfavoritesTotalByUserId(userId);
        userInfoDto.setTotalCollection(integer1 + integer2 + integer3 + integer4);


//        购物车数量
        Integer integer = shoppingcartService.shoppingcartTotalByUserId(userId);

        userInfoDto.setTotalShopping(integer);

//        余额数量

        BigDecimal overageByUserId = findOverageByUserId(userId);
        userInfoDto.setUserInfoOverage(overageByUserId);

//        汽车信息
        Usercar usercarByUserIdUsercarId = usercarService.findUsercarByUserIdUsercarId(userId);

        if (ObjectUtil.isNull(usercarByUserIdUsercarId)) {
            return userInfoDto;
        }
        userInfoDto.setUsercarId(usercarByUserIdUsercarId.getUsercarId());

        userInfoDto.setUsercarImage(usercarByUserIdUsercarId.getUsercarImage());

        userInfoDto.setCarbrandName(usercarByUserIdUsercarId.getCarbrandName());

        userInfoDto.setCarmodelName(usercarByUserIdUsercarId.getCarmodelName());

        userInfoDto.setCardisplacementName(usercarByUserIdUsercarId.getCardisplacementName());

        return userInfoDto;
    }


    /*
     * 功能描述〈用户评论详情〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/16 8:24
     * @throws
     */

    @Override
    public UserCommentDto userConmmentByUserId(Integer userId) {
        UserCommentDto userCommentDto = userInfoMapper.userConmmentByUserId(userId);
        userCommentDto.setUserId(userId);
        return ObjectUtils.isEmpty(userCommentDto) ? null : userCommentDto;
    }


}
